package com.frank.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.frank.auth.application.convert.AuthUserDtoConvert;
import com.frank.auth.domain.bo.AuthUserBo;
import com.frank.auth.domain.service.UserDomainService;
import com.frank.auth.entity.AuthUserDto;
import com.frank.common.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/6
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserDomainService userDomainService;

    public UserController(UserDomainService userDomainService) {
        this.userDomainService = userDomainService;
    }

    /**
     * 注册
     *
     * @param authUserDto auth user
     * @return result
     */
    @PostMapping(value = "/register", consumes = "application/json")
    public Result<Boolean> register(@RequestBody AuthUserDto authUserDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("auth user register: params: {}", authUserDto);
            }
            Preconditions.checkNotNull(authUserDto.getUserName(), "The username cannot be empty");
            Preconditions.checkNotNull(authUserDto.getEmail(), "The email cannot be empty");
            Preconditions.checkNotNull(authUserDto.getPassword(), "The password cannot be empty");
            AuthUserBo authUserBo = AuthUserDtoConvert.INSTANCE.convertDtoToBo(authUserDto);
            return Result.ok(userDomainService.register(authUserBo));
        } catch (Exception e) {
            log.error("auth user register error: params: {}", authUserDto, e);
            return Result.fail("新增用户失败");
        }
    }

    @PutMapping(value = "/update", consumes = "application/json")
    public Result<Boolean> update(@RequestBody AuthUserDto authUserDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("auth user update: params: {}", authUserDto);
            }
            Preconditions.checkNotNull(authUserDto.getId(), "The id cannot be empty");
            AuthUserBo authUserBo = AuthUserDtoConvert.INSTANCE.convertDtoToBo(authUserDto);
            return Result.ok(userDomainService.update(authUserBo));
        } catch (Exception e) {
            log.error("auth user update error: params: {}", authUserDto, e);
            return Result.fail("更新用户失败");
        }
    }

    @DeleteMapping(value = "/delete/{userId}")
    public Result<Boolean> delete(@PathVariable Integer userId) {
        try {
            if (log.isInfoEnabled()) {
                log.info("auth user delete: params: {}", userId);
            }
            return Result.ok(userDomainService.delete(Long.valueOf(userId)));
        } catch (Exception e) {
            log.error("auth user delete error: params: {}", userId, e);
            return Result.fail("删除用户失败");
        }
    }


    @RequestMapping("/doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("captcha") String captcha) {
        try {
            if (log.isInfoEnabled()) {
                log.info("User login: params: {}", captcha);
            }
            return Result.ok(userDomainService.doLogin(captcha));
        }catch (Exception e){
            log.error("User login error, error: {}", e.getMessage(), e);
            return Result.fail("Login fail");
        }
    }

    @GetMapping("/getUserInfo")
    public Result<AuthUserDto> getUserInfo(@RequestParam  String username){
        try {
            if (log.isInfoEnabled()) {
                log.info("Get user info: params: {}", username);
            }
           AuthUserBo authUserBo =  userDomainService.getUserInfo(username);
            return Result.ok(AuthUserDtoConvert.INSTANCE.convertBoToDto(authUserBo));
        }catch (Exception e){
            log.error("Get user info error, error: {}", e.getMessage(), e);
            return Result.fail("Get user info fail");
        }
    }

    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @GetMapping("/logOut")
    public Result<Boolean> logOut(String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Logout: params: {}", userName);
            }
            StpUtil.logout(userName);
            return Result.ok(true);
        }catch (Exception e){
            log.error("Logout error, error: {}", e.getMessage(), e);
            return Result.fail("Logout fail");
        }

    }
}
